package com.pwc.conversionTool.dataAdaptor;

import java.util.*;

import com.pwc.conversionTool.entity.FilterCondition;
import com.pwc.conversionTool.entity.Formula;
import com.pwc.conversionTool.entity.FormulaCondition;

public class FormulaProcess {

	private List<String[]> tempRows = new ArrayList<String[]>();
	//private List<FormulaCondition> dataFormulaList;
	private IDataAdaptor allData;
	private static StringUtil SUtil = new StringUtil();
	private Formula formula;

	public FormulaProcess(IDataAdaptor allData, Formula formulaInput) {
		this.tempRows = allData.getContent();
		this.allData = allData;
		this.formula =formulaInput;
		//this.dataFormulaList = formulaInput.getFormula();
	}

	public IDataAdaptor Process() {
		if(formula!=null)
		{
			if (formula.getFormula() != null) {
				Iterator<FormulaCondition> iterator = formula.getFormula().iterator();
				while (iterator.hasNext()) {
					FormulaCondition oneFormulaCondition = iterator.next();
					tempRows = ProcessFormular(oneFormulaCondition, allData,
							tempRows);
				}
			}
		}
		
		allData.setContent(tempRows);

		return allData;

	}

	/**
	 * check empty
	 * 
	 * @param input
	 * @return
	 */
	private static boolean NotNullAndEmpty(String input) {
		return input != null && !input.equals("");
	}

	private static List<String[]> ProcessFormular(FormulaCondition formulaCon,
			IDataAdaptor allData, List<String[]> tempRows) {
		Iterator<String[]> iterator = tempRows.iterator();

		if (formulaCon != null) {
			while (iterator.hasNext()) {
				String[] i = iterator.next();
				int index = allData.getColumnIndex(formulaCon.getColumnName());
				if (!allData.getHeader()[index].equals(i[index])) {

					// string
					// function:toUpCase,toLowCase,subString,Ltrim,Rtrim,Trim,InitCap,get_area_code,get_phone_number,Replace
					if ("toUpCase".equals(formulaCon.getOperation())) {
						i[index] = i[index].toUpperCase();
					}

					if ("toLowCase".equals(formulaCon.getOperation())) {
						i[index] = i[index].toLowerCase();
					}

					if ("subString".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = SUtil.SubString(i[index],
								formulaCon.getValue1(), formulaCon.getValue2());
					}
					if ("Ltrim".equals(formulaCon.getOperation())) {
						i[index] = SUtil.ltrim(i[index]);
					}

					if ("Rtrim".equals(formulaCon.getOperation())) {
						i[index] = SUtil.rtrim(i[index]);
					}

					if ("Trim".equals(formulaCon.getOperation())) {
						i[index] = SUtil.trim(i[index]);
					}
					if ("InitCap".equals(formulaCon.getOperation())) {
						i[index] = SUtil.InitCap(i[index]);
					}
					if ("get_area_code".equals(formulaCon.getOperation())) {
						i[index] = SUtil.get_area_code(i[index]);
					}
					if ("get_phone_number".equals(formulaCon.getOperation())) {
						i[index] = SUtil.get_phone_number(i[index]);
					}

					if ("Replace".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = SUtil.Replace(i[index],
								formulaCon.getValue1(), formulaCon.getValue2());
					}
					// if ("Lpad".equals(formulaCon.getOperation()) &&
					// NotNullAndEmpty(formulaCon.getValue1())) {
					// i[index] = SUtil.Lpad(i[index], formulaCon.getValue1(),
					// formulaCon.getValue2());
					// }
					// if ("Rpad".equals(formulaCon.getOperation()) &&
					// NotNullAndEmpty(formulaCon.getValue1())) {
					// i[index] = SUtil.Rpad(i[index], formulaCon.getValue1(),
					// formulaCon.getValue2());
					// }

					// Number:add,minus,multiply,divide,mod,roundLength,truncateLength
					if ("add".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = NumberUtil.Add(i[index],
								formulaCon.getValue1());
					}
					if ("minus".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = NumberUtil.Minus(i[index],
								formulaCon.getValue1());
					}
					if ("multiply".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = NumberUtil.Multiply(i[index],
								formulaCon.getValue1());
					}
					if ("divide".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = NumberUtil.Divide(i[index],
								formulaCon.getValue1());
					}
					if ("mod".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = NumberUtil.Mod(i[index],
								formulaCon.getValue1());
					}
					if ("roundLength".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = NumberUtil.Round(i[index],
								formulaCon.getValue1());
					}
					if ("truncateLength".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = NumberUtil.Truncate(i[index],
								formulaCon.getValue1());
					}

					// Date:CurrentTime,AddDay,AddMonth,AddYear,MinusDay,MinusMonth,MinusYear
					// ------date f(x)----------
					if ("CurrentTime".equals(formulaCon.getOperation())) {
						i[index] = DateUtil.getCurrentTime();
					}
					if ("AddDay".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = DateUtil.addDay(i[index],
								formulaCon.getValue1());
					}
					if ("AddMonth".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = DateUtil.addMonth(i[index],
								formulaCon.getValue1());
					}
					if ("AddYear".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = DateUtil.addYear(i[index],
								formulaCon.getValue1());
					}
					if ("MinusDay".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = DateUtil.minusDay(i[index],
								formulaCon.getValue1());
					}
					if ("MinusMonth".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = DateUtil.minusMonth(i[index],
								formulaCon.getValue1());
					}
					if ("MinusYear".equals(formulaCon.getOperation())
							&& NotNullAndEmpty(formulaCon.getValue1())) {
						i[index] = DateUtil.minusYear(i[index],
								formulaCon.getValue1());
					}

				}
			}
		}
		return tempRows;

	}

}
